package com.example.shiXun.service.impl;

import com.example.shiXun.base.result.ResponseCode;
import com.example.shiXun.base.result.Results;
import com.example.shiXun.dao.RoleDao;
import com.example.shiXun.dao.RolePermissionDao;
import com.example.shiXun.dao.RoleUserDao;
import com.example.shiXun.domain.SysRole;
import com.example.shiXun.domain.SysRoleUser;
import com.example.shiXun.dto.RoleDto;
import com.example.shiXun.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：meet
 * @date ：Created in 2020/4/12 1:20
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDao roleDao;

    @Autowired
    private RoleUserDao roleUserDao;

    @Resource
    private RolePermissionDao rolePermissionDao;

    @Override
    public Results<SysRole> getAllRole() {
        return Results.success(50,roleDao.getAllRole());
    }

    @Override
    public Results<SysRole> getAllRoleByPage(Integer offset, Integer limit) {
        return Results.success(roleDao.countAllRole().intValue() , roleDao.getAllRoleByPage(offset,limit));
    }

    @Override
    public Results<SysRole> getRoleByFuzzyName(String roleName, Integer offset, Integer limit) {
        return Results.success(roleDao.countRoleByFuzzyName(roleName).intValue(),roleDao.getRoleByFuzzyNamePage(roleName,offset,limit));
    }

    @Override
    public Results<SysRole> save(RoleDto roleDto) {
        //1、先保存角色"
        roleDao.saveRole(roleDto);
        //移除0,permission id是从1开始
        List<Long> permissionIds = roleDto.getPermissionIds();
        permissionIds.remove(0L);
        //2、保存角色对应的所有权限
        if (!CollectionUtils.isEmpty(permissionIds)){
            rolePermissionDao.save(roleDto.getId(),permissionIds);
        }
        return Results.success();

    }

    @Override
    public SysRole getRoleById(Integer id) {
        return roleDao.getById(id);
    }

    @Override
    public Results update(RoleDto roleDto) {
        List<Long> permissionIds = roleDto.getPermissionIds();
        permissionIds.remove(0L);
        rolePermissionDao.deleteRolePermission(roleDto.getId());

        if (!CollectionUtils.isEmpty(permissionIds)){
            rolePermissionDao.save(roleDto.getId(),permissionIds);
        }

        int countData = roleDao.update(roleDto);
        if (countData>0){
            return Results.success();
        }else {
            return Results.failure();
        }
    }

    @Override
    public Results delete(Integer id) {
        List<SysRoleUser> datas = roleUserDao.listAllSysRoleUserByRoleId(id);
        if (datas.size()<=0){
            roleDao.delete(id);
            return Results.success();
        }
        return Results.failure(ResponseCode.USERNAME_REPEAT.USER_ROLE_NO_CLEAR.getCode(),ResponseCode.USERNAME_REPEAT.USER_ROLE_NO_CLEAR.getMessage());
    }
}
